import { request } from "@tarojs/taro";
import { ActionResult } from "./type";

export default class ApiRequest {
  private readonly baseUrl: string;
  private readonly header = { "content-type": "application/json" };

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  get<IData = any, ResultData = any>(url: string) {
    return async (data?: IData): Promise<ActionResult<ResultData>> => {
      const res = await request({
        url: `${this.baseUrl}${url}`,
        method: "GET",
        data: data || {},
        header: { ...this.header },
      });
      return res.data;
    };
  }

  post<IData = any, ResultData = any>(url: string) {
    return async (data?: IData): Promise<ActionResult<ResultData>> => {
      const res = await request({
        url: `${this.baseUrl}${url}`,
        method: "POST",
        data: data || {},
        header: { ...this.header },
      });
      return res.data;
    };
  }
}
